/**
 * KAYO APPLIANCE CHAINS.
 * Copyright (c) 2016-2016 All Rights Reserved.
 */
package com.leku.video.controller.video;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.leku.commons.util.StringUtil;
import com.leku.video.base.BaseController;
import com.leku.video.constants.ResultEnum;
import com.leku.video.dto.request.video.VideoAddrReqDto;
import com.leku.video.dto.request.video.VideoReqDto;
import com.leku.video.dto.result.ResultDto;
import com.leku.video.dto.result.video.VideoResDto;
import com.leku.video.dto.result.video.VideoUrlResDto;
import com.leku.video.service.video.VideoService;
import com.leku.video.util.ServletUtil;

/**
 * 
 * 功能描述:视频服务
 *
 * @author Ranphy
 * @created 2018年9月20日
 * @version 1.0.0
 */
@Controller
public class VideoController extends BaseController {

	private static final Logger LOG = LoggerFactory.getLogger(VideoController.class);

	@Autowired
	private VideoService videoService;

	/**
	 * 
	 * 功能描述: 视频详情
	 *
	 * @param request
	 * @param response
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@RequestMapping("/video/details")
	@ResponseBody
	public void videoDetail(HttpServletRequest request, HttpServletResponse response) {

		ResultDto<VideoResDto> resultDto = new ResultDto<VideoResDto>();

		VideoReqDto reqDto = preVideoReqDto(request);

		// 参数校验不通过
		if (!checkBaseParam(reqDto) || StringUtil.isEmpty(reqDto.getId())) {
			resultDto.setRet(ResultEnum.PARAMERROR.getResCode());
			resultDto.setMsg(ResultEnum.PARAMERROR.getResMsg());
			writeReponse(request, response, resultDto);
			return;
		}

		try {
			resultDto.setData(videoService.findVideoDetail(reqDto));
			resultDto.setRet(ResultEnum.SUCCESS.getResCode());
			resultDto.setMsg(ResultEnum.SUCCESS.getResMsg());
		} catch (Exception e) {
			resultDto.setRet(ResultEnum.SYSERROR.getResCode());
			resultDto.setMsg(ResultEnum.SYSERROR.getResMsg());
			LOG.error("获取视频信息数据系统异常," + getAppInfo(reqDto), e);
		}

		writeReponse(request, response, resultDto);
	}

	/**
	 * 
	 * 功能描述: 视频地址
	 *
	 * @param request
	 * @param response
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@RequestMapping("/video/addrs")
	@ResponseBody
	public void videoAddrs(HttpServletRequest request, HttpServletResponse response) {

		ResultDto<VideoUrlResDto> resultDto = new ResultDto<VideoUrlResDto>();

		VideoAddrReqDto reqDto = preVideoAddrReqDto(request);

		// 参数校验不通过
		if (!checkBaseParam(reqDto) || StringUtil.isEmpty(reqDto.getId())) {
			resultDto.setRet(ResultEnum.PARAMERROR.getResCode());
			resultDto.setMsg(ResultEnum.PARAMERROR.getResMsg());
			writeReponse(request, response, resultDto);
			return;
		}

		try {
			resultDto.setData(videoService.findVideoAddrs(reqDto));
			resultDto.setRet(ResultEnum.SUCCESS.getResCode());
			resultDto.setMsg(ResultEnum.SUCCESS.getResMsg());
		} catch (Exception e) {
			resultDto.setRet(ResultEnum.SYSERROR.getResCode());
			resultDto.setMsg(ResultEnum.SYSERROR.getResMsg());
			LOG.error("获取视频信息数据系统异常," + getAppInfo(reqDto), e);
		}

		writeReponse(request, response, resultDto);
	}

	/**
	 * 
	 * 功能描述:视频请求封装
	 *
	 * @param reqDto
	 * @param request
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	private VideoReqDto preVideoReqDto(HttpServletRequest request) {
		VideoReqDto reqDto = new VideoReqDto();

		reqDto.setId(request.getParameter("id"));

		// 组装基本参数
		preBaseDto(reqDto, request);

		return reqDto;
	}

	/**
	 * 
	 * 功能描述:视频地址请求封装
	 *
	 * @param reqDto
	 * @param request
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	private VideoAddrReqDto preVideoAddrReqDto(HttpServletRequest request) {
		VideoAddrReqDto reqDto = new VideoAddrReqDto();

		reqDto.setId(request.getParameter("id"));
		reqDto.setSeg(request.getParameter("seg"));

		// 组装基本参数
		preBaseDto(reqDto, request);

		return reqDto;
	}
}
